package com.zhn.evsi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhn.evsi.domain.dto.evsi.EvsiPlanetSelectDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiPlanetTreeDTO;
import com.zhn.evsi.domain.dto.evsi.EvsiPlanetTreeInfoDTO;
import com.zhn.evsi.domain.entity.evsi.EvsiInvtypes;
import com.zhn.evsi.domain.entity.evsi.EvsiPlanetSchematicStypeMap;
import com.zhn.evsi.domain.entity.evsi.EvsiPlanetSchematics;
import com.zhn.evsi.domain.entity.evsi.EvsiPlanetSchematicsPinMap;
import com.zhn.evsi.mapper.EvsiPlanetSchematicsMapper;
import com.zhn.evsi.service.IEvsiInvtypesService;
import com.zhn.evsi.service.IEvsiPlanetSchematicStypeMapService;
import com.zhn.evsi.service.IEvsiPlanetSchematicsPinMapService;
import com.zhn.evsi.service.IEvsiPlanetSchematicsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

@Service
public class EvsiPlanetSchematicsServiceImpl extends ServiceImpl<EvsiPlanetSchematicsMapper, EvsiPlanetSchematics> implements IEvsiPlanetSchematicsService {

    @Autowired
    private IEvsiPlanetSchematicStypeMapService planetSchematicStypeMapService;

    @Autowired
    private IEvsiPlanetSchematicsPinMapService planetSchematicsPinMapService;

    @Autowired
    private IEvsiInvtypesService invtypesService;

    // 行星产物树状结构
    @Override
    public EvsiPlanetTreeDTO planetTree(Integer schematicID, Integer quantity) {
        EvsiPlanetTreeDTO evsiPlanetTreeDTO = new EvsiPlanetTreeDTO();
        EvsiPlanetSchematics planetSchematics = this.getOne(new LambdaQueryWrapper<EvsiPlanetSchematics>().eq(EvsiPlanetSchematics::getSchematicId, schematicID));

        evsiPlanetTreeDTO.setSchematicID(schematicID);
        evsiPlanetTreeDTO.setSchematicName(planetSchematics.getSchematicName());
        evsiPlanetTreeDTO.setCycleTime(planetSchematics.getCycleTime());
        evsiPlanetTreeDTO.setQuantity(quantity);
        evsiPlanetTreeDTO.setLevel(planetSchematics.getLevel());
        evsiPlanetTreeDTO.setEvsiPlanetTreeInfoDTOList(evsiPlanetTreeInfoDTOList(schematicID, quantity));
        return evsiPlanetTreeDTO;
    }

    private List<EvsiPlanetTreeInfoDTO> evsiPlanetTreeInfoDTOList(Integer schematicID, Integer quantity) {
        List<EvsiPlanetTreeInfoDTO> evsiPlanetTreeInfoDTOList = new ArrayList<>();

        List<EvsiPlanetSchematicStypeMap> planetSchematicStypeMapList = planetSchematicStypeMapService.list(new LambdaQueryWrapper<EvsiPlanetSchematicStypeMap>()
                .eq(EvsiPlanetSchematicStypeMap::getSchematicId, schematicID).eq(EvsiPlanetSchematicStypeMap::getIsInput, 1));

        planetSchematicStypeMapList.forEach(v -> {
            // 通过typeid获取下一级物品信息
            EvsiInvtypes invtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, v.getTypeId()));
            // 通过物品名称对应起来物品id
            EvsiPlanetSchematics planetSchematics = this.getOne(new LambdaQueryWrapper<EvsiPlanetSchematics>().eq(EvsiPlanetSchematics::getSchematicName, invtypes.getTypeName()));

            EvsiPlanetTreeInfoDTO evsiPlanetTreeInfoDTO = new EvsiPlanetTreeInfoDTO();

            evsiPlanetTreeInfoDTO.setTypeID(v.getTypeId());
            evsiPlanetTreeInfoDTO.setQuantity(v.getQuantity() * quantity);
            evsiPlanetTreeInfoDTO.setIsInput(v.getIsInput());
            evsiPlanetTreeInfoDTO.setGroupID(invtypes.getGroupId());
            evsiPlanetTreeInfoDTO.setTypeName(invtypes.getTypeName());
            evsiPlanetTreeInfoDTO.setDescription(invtypes.getDescription());
            evsiPlanetTreeInfoDTO.setVolume(invtypes.getVolume());
            evsiPlanetTreeInfoDTO.setIconID(invtypes.getIconId());
            evsiPlanetTreeInfoDTO.setSchematicID(planetSchematics.getSchematicId());
            if (planetSchematics != null && !Objects.equals(planetSchematics.getLevel(), "P1")) {

                evsiPlanetTreeInfoDTO.setLevel(planetSchematics.getLevel());
                evsiPlanetTreeInfoDTO.setEvsiPlanetTreeDTO(planetTree(planetSchematics.getSchematicId(), v.getQuantity() * quantity));
            } else {
                evsiPlanetTreeInfoDTO.setLevel("P1");
                List<EvsiPlanetSchematicsPinMap> planetSchematicsPinMapList = planetSchematicsPinMapService.list(new LambdaQueryWrapper<EvsiPlanetSchematicsPinMap>()
                                .eq(EvsiPlanetSchematicsPinMap::getSchematicId, planetSchematics.getSchematicId()));

                planetSchematicsPinMapList.forEach(k -> {
                    EvsiInvtypes evsiInvtypes = invtypesService.getOne(new LambdaQueryWrapper<EvsiInvtypes>().eq(EvsiInvtypes::getTypeId, k.getPinTypeId()));
                    k.setPinTypeName(evsiInvtypes.getTypeName());
                });
                evsiPlanetTreeInfoDTO.setEvsiPlanetSchematicsPinMaps(planetSchematicsPinMapList);
            }

            evsiPlanetTreeInfoDTOList.add(evsiPlanetTreeInfoDTO);
        });

        return evsiPlanetTreeInfoDTOList;
    }


    // 级联选择器
    @Override
    public List<EvsiPlanetSelectDTO> planetSelect(String level) {
        return this.baseMapper.planetSelect(level);
    }

}
